<?php
/**
 * @Author: Awe
 * @Date:   2016-05-15 23:49:50
 * @Last Modified by:   Awe
 * @Last Modified time: 2019-03-25 13:23:13
 * @des 后台的用户 业务文件
 */
class AdminUserBusiness extends AbstractModel {
    public function findUser($appid , $appsecret){
        $adminUser = new AdminUserModel();
        $info = $adminUser->getDataByAppidAppsecret($appid , $appsecret);
        if(!$info || !is_array($info)){
            return array('code' => 0 , 'msg' =>"没有查询到用户") ;
        }
        /*if($info['status'] == 2 ){
            return array('code' => 0 , 'msg'=>'此用户已经被禁用');
        }
        if($info['status'] == 0 ){
            return array('code' => 0 , 'msg'=>'审核中。。。。');
        }*/
        //修改字段登录次数
        return array('code' => 1 , 'msg'=>'success' , 'info'=>$info) ;
    }
    //写入登录状态,这个是针对 权限管理系统的登录帐号
    public function setUserCookie($cid){
        $AdminUserModel = new AdminUserModel();
        $userInfo = $AdminUserModel->getAdminUserByUserId($cid);
        if( empty($userInfo) ){
            return false;
        }
        global $_G ;
        $cookieData = array();
        $cookieData['nick'] = ($userInfo['cname'])  ? $userInfo['cname']:$userInfo['cname'] ;
        $cookieData['cid'] = $userInfo['cid'];
        $cookieData['appid'] = $userInfo['appid'];
        $cookieData['notice_url'] = $userInfo['notice_url'];
        $cookieData['super'] = 1;
        $data_string = serialize($cookieData) ;
        $data_string = Encrypt::AuthCode($data_string , "ENCODE" ,$_G['config']['encrypt']['encrypt']['key']);
        setcookie("bcr_admin_auth", $data_string, time()+intval(3600*72), "/");
        return array('code' => 1 , 'message'=>'success');
    }
    public function getAdminUserList($params = array(),$pageSize ){
        $page = isset($params['page']) ? $params['page'] : 1 ;
        $getWhere = " where 1 = 1  ";
        $getWhere.=$this->getAdminUserWhere($params);
        $sql = "select a.*  from admin_user as a   {$getWhere} order by a.addtime desc "   ;
        $sql_count = "select count(*) as c   from admin_user as a  {$getWhere} " ;
        return $this->getCommonDataList($sql , $sql_count , $pageSize,$page,0);
    }
    //获取查询的条件
    public function getAdminUserWhere($params=array()){
        $getWhere = "";
        if(isset($params['username']) && $params['username']){
            $getWhere.=" and a.`username` = '{$params['username']}' ";
        }
        if(isset($params['super']) && $params['super']){
            $getWhere.=" and a.`super` = '{$params['super']}' ";
        }
        return $getWhere;
    }
    public function editBase( $req , $cid  ){
        $AdminUserModel = new AdminUserModel();
        $userInfo = $AdminUserModel->getAdminUserByUserId($cid);
        if( empty($userInfo) ){
            return array('code' => 0 ,'msg' => "没有查询到客户信息" );
        }
        $status = $AdminUserModel->updateCustomerUser($req,array('cid' => $cid));
        if( !$status ){
            return array('code' => 0 ,'msg' => "网络繁忙请稍后" );
        }
        return array('code' => 1,'msg' => "修改成功" );
    }
}